<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class FrontBaseController extends Controller {
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout = '//layouts/main';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu = array ();
	public $menuID		= '';//网站主菜单的ID {home:首页， meishi:美食, xxyl:休闲娱乐, lycx:旅游出行， zhusu:住宿， shfw:生活服务， gouwu:购物， member_center:会员中心， member_card:会员卡， discount:打折资讯， shop:合作商家}
	
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs = array ();
	public $logged = 0; //此次请求是否已经做日志记录,0为未做
	public $userType = 2; //用户类型，1表示为后台用户，2表示前台用户，0表示未知
	
	public function init() {
		
		parent::init ();
		
		$router = strtolower ( $this->getRouteI () );
		
		
	}
	
	public function filters() {
		return array ('RequestI' );
	}
	
	/*
     * 判断登录过滤器
     *
     */
	
	public function filterIsLogin($filterChain) {
		if (Yii::app ()->frontuser->isGuest) {
			$this->redirect ( '/?r=user/login' );
		}
		$filterChain->run ();
	}
	
	/*
     * post提交过滤
     *
     */
	
	public function filterIsPost($filterChain) {
		if (! Yii::app ()->request->getIsPostRequest ()) {
			throw new CHttpException ( 403, '非法操作！' );
		}
		$filterChain->run ();
	}
	
	/*
     * Ajax提交过滤
     *
     */
	
	public function filterIsAjax($filterChain) {
		if (! Yii::app ()->request->getIsAjaxRequest ()) {
			throw new CHttpException ( 403, '非法操作！' );
		}
		$filterChain->run ();
	}
	
	
	
	/*
	*获取缓存的ajax内容，js动态请求时使用
	*@param bool $isSubSite 内容是否区分站点
	*@param bool $getLast 不从缓存里去，取最新的
	*@param array $exculdeParams uri中或者$_POST中例外的参数，表示这个参数不会作为获取缓存的key的参数
	*@param array $extraParams 额外参数，作为数组元素传进来参数值
	*@return max 获取成功返回内容,否则返回false或者null
	*/
	public function getCacheAjaxContents($isSubSite = true, $getLast = false, $exculdeParams = array(), $extraParams = array()) {
		$return = false;
		if (! $getLast) {
			$key = $this->genAjaxCacheKey ( $isSubSite, $exculdeParams, $extraParams );
			$return = UtilD::getCache ( 'ajaxcache', $key );
		}
		return $return;
	}
	
	/*
	*获取缓存的ajax内容，js动态请求时使用
	*@param string $content 要返回给ajax请求的内容，也就是要做缓存的内容
	*@param bool $isSubSite 内容是否区分站点
	*@param array $exculdeParams uri中或者$_POST中例外的参数名，表示这个参数不会作为获取缓存的key的参数,譬如用getJSON时的callback参数
	*@param array $extraParams 额外参数，作为数组元素传进来参数值
	*@param int $cacheTime 缓存时间
	*@return bool 设置成功返回true,否则返回false
	*/ 
	public function setCacheAjaxContents($content, $isSubSite = true, $exculdeParams = array(), $extraParams = array(), $cacheTime = 600) {
		$key = $this->genAjaxCacheKey ( $isSubSite, $exculdeParams, $extraParams );
		return UtilD::setCache ( 'ajaxcache', $key, $content, $cacheTime );
	}
	
	private function genAjaxCacheKey($isSubSite = true, $exculdeParams = array(), $extraParams = array()) {
		$key = $isSubSite ? 'ajax_cache_' . $this->siteAreaCode : 'ajax_cache';
		$requestGET = isset ( $_GET ) ? $_GET : array ();
		$requestPOST = isset ( $_POST ) ? $_POST : array ();
		if (is_array ( $exculdeParams )) {
			foreach ( $exculdeParams as $ak ) {
				isset ( $requestGET [$ak] ) && $requestGET [$ak] = '';
				isset ( $requestPOST [$ak] ) && $requestPOST [$ak] = '';
			}
		}
		if (is_array ( $extraParams )) {
			foreach ( $extraParams as $ev ) {
				$key .= $ev;
			}
		}
		$key .= $_SERVER ['SCRIPT_NAME'];
		isset ( $requestGET ) && ! empty ( $requestGET ) && $key .= serialize ( $requestGET );
		isset ( $requestPOST ) && ! empty ( $requestPOST ) && $key .= serialize ( $requestPOST );
		/*if($this->siteAreaCode == '101120103'){
            file_put_contents('/home/wwwroot/www/haha.log',md5($key)."\r\n",FILE_APPEND);
		}*/
		return md5 ( $key );
	}
	

}